package com.example.android2019.homework.homework6;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;

/**
 * Created by 一世长安一世欢 on 2019/6/9.
 */

public class SPSave {
    /**
     * @param context 上下文对象
     * @param height  身高
     * @param weight  体重
     * @return 保存成功返回true
     */
    public static boolean save(Context context, String height, String weight) {
        SharedPreferences sp = context.getSharedPreferences("sg1", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("height", height);
        editor.putString("weight", weight);
        editor.commit();
        return true;
    }

    /**
     * 获取sp中的内容，保存在hashmap并返回
     *
     * @param context 上下文对象
     * @return
     */
    public static HashMap<String, String> get(Context context) {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        SharedPreferences sp = context.getSharedPreferences("sg1", Context.MODE_PRIVATE);
        String height = sp.getString("height", null);
        String weight = sp.getString("weight", null);
        hashMap.put("height", height);
        hashMap.put("pwd", weight);
        return hashMap;
    }
}
